<?php
/*
This script updates the metadata json file for a Google Refine project / table
it indicates that a user decided to SKIP documentation of a given column / field
*/

require_once( 'core.php' );
require_once( 'current_user_api.php' );
require_once( 'bugnote_api.php' );

auth_ensure_user_authenticated( );
//helper_begin_long_process( );


# extracts the user information for the currently logged in user
# and prefixes it with u_
$f_user_id = gpc_get_int( 'id', auth_get_current_user_id() );
$row = user_get_row( $f_user_id );

extract( $row, EXTR_PREFIX_ALL, 'u' );
$u_email = user_get_email( $u_id );
$u_realname = user_get_realname( $u_id );




$t_plugin_path = config_get( 'plugin_path' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'TableMetadata.php' );
require_once( $t_plugin_path . 'DataRefine' . DIRECTORY_SEPARATOR . 'JSONstore.php' );

$refineURI = plugin_config_get( 'refineLocalURI' );
if(!$refineURI){
	$refineURI = plugin_config_get( 'refineBaseURI' );
}


$projectID = $_POST["grProject"]; 
$colIndex = $_POST["colIndex"]+0;
$vocabURI = $_POST["vocabURI"]; 
$vocabName = $_POST["vocabName"];
$conceptURI = $_POST["conceptURI"]; 
$conceptName = $_POST["conceptName"]; 

$tableMetaObj = new TableMetadata;
$tableMetaObj->refineURI = $refineURI;
$tableMetaObj->currentUser = $f_user_id;
$tableMetaObj->GRprojectID = $projectID;
$tableMetaObj->get_metadata();
$tableMetaObj->add_User_Role($f_user_id, $u_username, "field");
$tableMetadata = $tableMetaObj->currentMetadata;

$newColData = $tableMetadata["columns"];

if(stristr($vocabURI, "http://") || stristr($conceptURI, "http://")){
	
	$addedConcept = array("vocabURI" => $vocabURI,
			"vocabName" => $vocabName,
			"conceptURI" => $conceptURI,
			"conceptName" => $conceptName,
			"noteCreator" => $u_username);
	
	$newColData[$colIndex]["concept"] = $addedConcept;
}
else{
	$addedConcept = false;
	if(isset($newColData[$colIndex]["concept"])){
		//delete the measurement unit assignment
		unset($newColData[$colIndex]["concept"]);
	}
}


$tableMetadata["columns"] = $newColData;

$json = json_encode($tableMetadata, 1);
$JSONstore = new JSONstore;
$JSONstore->setDirectory("metadata");
$JSONstore->save_json($projectID, $json);

$output = json_encode(array("colIndex" => $colIndex,
			    "success" => true,
			    "addedConcept" => $addedConcept), 1);


/*
$bugNote = "No note needed for column: ".$tableMetadata["columns"][$colIndex]["name"];

	require_once( 'bug_api.php' );
	require_once( 'bugnote_api.php' );

	$f_bug_id		= $tableMetadata["bugIDs"][0];
	$f_private		= false;
	$f_time_tracking	= null;
	$f_bugnote_text	= trim($bugNote);
	$t_bugnote_id = bugnote_add( $f_bug_id, $bugNote, $f_time_tracking, $f_private, BUGNOTE );
*/


//now output for AJAX call from the Refine Login Screen
if(!isset($_GET["callback"])){
	header('Content-Type: application/json; charset=utf8');
	echo $output;
}
else{
	header('Content-Type: application/json; charset=utf8');
	$callback = $_GET["callback"];
	echo $callback."(".$output.");";
}


?>
